PKG_DIR=../../../device/board/unionman/unionpi_lion/common/images/pkg

IMAGE_DIR := $(ROOT_OUT_DIR)/
PACK_OUT := build/pack
RSA_OUT := build/rsa
U_BOOT_LOAD_ADDR := 0x43000000
MKIMAGE_OPTS := -A arm -O u-boot -T firmware -C none -a $(U_BOOT_LOAD_ADDR) -e 0 -n "U-Boot 2016.11 for lombo board" -z leaver
BOOTFS_DIR := bootfs
BOOTFS_IMAGE := bootfs.img
BOOTFS_SIZE := 8192
MKBOOTFS_OPTS := -F 12 -n "BOOTFS"
ENV_CFG ?= env.cfg
DATE := `date +%F`
TARGET_PRODUCT := unionpi_lion
KERNEL_DTB ?= n7v3_sv823_demoboard.dtb
PKG_NAME := $(TARGET_PRODUCT)-$(DATE).pkg
UPKG_NAME := $(TARGET_PRODUCT)-update-$(DATE).pkg

#all: eipkg_prepare eibooster eileaver eileaver_env
all: eipkg
eipkg_prepare:	
	echo "************************* $(PKG_DIR)"
	rm -rf $(PACK_OUT) $(RSA_OUT)
	mkdir -p $(PACK_OUT)
	mkdir -p $(RSA_OUT)
	cp -f $(PKG_DIR)/sv823_bootloader/* $(PACK_OUT)
	cp -f $(PKG_DIR)/pack/* $(PACK_OUT)
	cp -f $(PKG_DIR)/param_pack/* $(PACK_OUT)
	cp -f $(PKG_DIR)/rsa/* $(RSA_OUT)
	cp -rf $(PKG_DIR)/frontier_bootloader/* $(PACK_OUT)
	cp -rf $(PKG_DIR)/configs/* $(PACK_OUT)
	cp -f $(PKG_DIR)/n7v3_frontier_823_param.json $(PACK_OUT)/param.json

eibooster: eipkg_prepare	
	boot_data 2 $(PACK_OUT)/ $(RSA_OUT)/ $(PACK_OUT)/param_pack.json > /dev/null
	rm -f booster_tmp.img*
	@if [ ! -f $(PACK_OUT)/booster.img ] ; then echo "Generate $(PACK_OUT)/booster.img failed" ; exit 2 ; fi
	
	cp $(PACK_OUT)/booster.img $(IMAGE_DIR)
	rsa_verify $(RSA_OUT)/root_pbk.key $(PACK_OUT)/booster_tmp.img $(RSA_OUT)/root.hash

.PHONY: leaver_json
leaver_json:
	replace.py $(PACK_OUT)/leaver.json info dir $(PACK_OUT)


define gen-imbr-bin
@echo "$(PACK_OUT)/imbr_mmc.json"
@echo "$(PACK_OUT)/imbr_mmc.bin"
cp rom_bin/* $(PACK_OUT)

-iMBR $(PACK_OUT)/imbr_mmc.json $(PACK_OUT)/imbr_mmc.bin
@if [ ! -f $(PACK_OUT)/imbr_mmc.bin ] ; then echo "Generate $(PACK_OUT)/imbr_mmc.bin failed" ; exit 2 ; fi
-iMBR $(PACK_OUT)/imbr_nand.json $(PACK_OUT)/imbr_nand.bin > /dev/null
@if [ ! -f $(PACK_OUT)/imbr_nand.bin ] ; then echo "Generate $(PACK_OUT)/imbr_nand.bin failed" ; exit 2 ; fi
-iMBR $(PACK_OUT)/imbr_nor.json $(PACK_OUT)/imbr_nor.bin > /dev/null
@if [ ! -f $(PACK_OUT)/imbr_nor.bin ] ; then echo "Generate $(PACK_OUT)/imbr_nor.bin failed" ; exit 2 ; fi
-iMBR $(PACK_OUT)/imbr_spi_nand.json $(PACK_OUT)/imbr_spi_nand.bin > /dev/null
@if [ ! -f $(PACK_OUT)/imbr_spi_nand.bin ] ; then echo "Generate $(PACK_OUT)/imbr_spi_nand.bin failed" ; exit 2 ; fi
endef

define encrypt_img
openssl enc -nosalt -nopad -aes-256-cbc -in $(1) -out $(2) -K CF66EADD1085DB5EC0BDC8933677B72BCF66EADD1085DB5EC0BDC8933677B72B -iv 3D724F83587F7D8A2487C572E5E98B7A
endef

LEAVER_BOOT := leaver_boot-nodtb.img

.PHONY: eileaver
eileaver: eipkg_prepare leaver_json	
	$(gen-imbr-bin)

	file_size=`stat -c %s $(PACK_OUT)/$(LEAVER_BOOT)`; \
	aligned_size=$$((($$file_size + 16 -1)/16 * 16)); \
	dd if=/dev/zero of=$(PACK_OUT)/leaver_align_zero.img bs=1 count=$$aligned_size
	dd if=$(PACK_OUT)/$(LEAVER_BOOT) of=$(PACK_OUT)/leaver_align_zero.img bs=1 conv=notrunc
	mv $(PACK_OUT)/leaver_align_zero.img $(PACK_OUT)/leaver_boot-nodtb.img.align

	@echo "encrypting leaver_boot.img"
	$(call encrypt_img,$(PACK_OUT)/leaver_boot-nodtb.img.align,$(PACK_OUT)/leaver_boot-nodtb.img.enc)
	mkimage $(MKIMAGE_OPTS) -d $(PACK_OUT)/leaver_boot-nodtb.img.align $(PACK_OUT)/leaver_boot.img

	@echo "signing leaver_boot.img"
	rsa_sign_pss $(RSA_OUT)/ntw_keypair.key $(PACK_OUT)/leaver_boot.img
	dd if=$(PACK_OUT)/leaver_boot-nodtb.img.enc of=$(PACK_OUT)/leaver_boot.img seek=64 bs=1
	mv $(PACK_OUT)/leaver_boot.img $(PACK_OUT)/leaver_boot_enc.img
	cat $(PACK_OUT)/leaver_boot_enc.img $(PACK_OUT)/leaver_boot.img.sig > $(PACK_OUT)/leaver_boot_es.img

	CometPack $(PACK_OUT)/leaver.json $(PACK_OUT)/leaver.img
	@if [ ! -f $(PACK_OUT)/leaver.img ] ; then echo "Generate $(PACK_OUT)/leaver.img failed" ; exit 2 ; fi
	cp $(PACK_OUT)/leaver.img $(IMAGE_DIR)

PHONY: eileaver_env
eileaver_env: eipkg_prepare
	mkenvimage -s 0x20000 -o $(PACK_OUT)/leaver_env.img $(PACK_OUT)/$(ENV_CFG)
	cp $(PACK_OUT)/leaver_env.img $(IMAGE_DIR)


.PHONY: eibootfs
eibootfs: eipkg_prepare	
	dd if=/dev/zero of=$(IMAGE_DIR)/$(BOOTFS_IMAGE) bs=512 count=$(BOOTFS_SIZE)
	mkfs.fat $(IMAGE_DIR)/$(BOOTFS_IMAGE) $(MKBOOTFS_OPTS)
	mcopy -i $(IMAGE_DIR)/$(BOOTFS_IMAGE) -/ $(BOOTFS_DIR)/* ::/
#	rm -rf $(BOOTFS_DIR)

PACK_JSON := $(PACK_OUT)/pack/nmlpack.json
UPDATE_JSON := $(PACK_OUT)/update.json
UPDATE_CFG := update_cfg.json
ROOTFS_NAME := root
ROOTFS_IMAGE := rootfs.ext4

define update_pack_json
cleanup.py $(1)
replace.py $(1) info dir $(PACK_OUT)
replace.py $(1) section update_cfg $(UPDATE_CFG)
replace.py $(1) section dtbb ../../kernel/OBJ/linux-4.19/arch/arm/boot/dts/lombo/n7v3/$(KERNEL_DTB)
replace.py $(1) section dtb ../../kernel/OBJ/linux-4.19/arch/arm/boot/dts/lombo/n7v3/$(KERNEL_DTB)
replace.py $(1) section env ../../leaver_env.img
replace.py $(1) section bootfs ../../$(BOOTFS_IMAGE)
replace.py $(1) section kernel ../../kernel/OBJ/linux-4.19/arch/arm/boot/uImage
replace.py $(1) section $(ROOTFS_NAME) ../../$(ROOTFS_IMAGE)
replace.py $(1) section userfs ../../userfs.ext4
endef

.PHONY: pack_json
pack_json: eipkg_prepare
	$(call update_pack_json, $(PACK_JSON))

eipkg: eipkg_prepare eibooster eileaver eileaver_env eibootfs pack_json
	CometPack $(PACK_JSON) $(PACK_OUT)/$(PKG_NAME)
	if [ ! -f $(PACK_OUT)/$(PKG_NAME) ] ; then echo "Generate $(PACK_OUT)/$(PKG_NAME) failed" ; exit 2 ; fi
	mv $(PACK_OUT)/$(PKG_NAME) $(IMAGE_DIR)
	@echo -e "\nGenerate \033[;32;1m$(PKG_NAME)\033[0m\n"
